package com.wjf.birdfish.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;




public class SystemCommand {
	
	private static Logger LOG = Logger.getLogger(SystemCommand.class.getName());
	
	public static void main(String[] args){
		boolean result = copy("132.42.33.89","tomcat","passw0rd","/home/tomcat/lzp_bak","/home/tomcat/lzp/dir1/dir2/");
		System.out.println(result);
	}
	
	public static boolean copy(String fromPath, String toPath,boolean isFile) {
		LOG.debug("SystemCommand:copy begin,fromPath="+fromPath+",toPath="+toPath);
		long beginTime = System.currentTimeMillis();
		boolean result = false;
		if(StringUtils.isBlank(fromPath) || StringUtils.isBlank(toPath)){
			LOG.error("SystemCommand:copy error,fromPath or toPath is blank");
			return result;
		}
		if(isFile){
			try {
				FileUtil.createPathAndFile(toPath);
			} catch (IOException e) {
				LOG.error("SystemCommand:copy error,"+e.getMessage());
				return result;
			}
		}else{
			try {
				File file = new File(toPath);
				result = file.mkdirs();
				if(!result){
					LOG.error("SystemCommand:copy error,create path error,path="+toPath);
					return result;
				}
			} catch (Exception e1) {
				LOG.error("SystemCommand:copy error,create path error,path="+toPath,e1);
				return result;
			}
		}
		
		InputStream ins = null;
		ByteArrayOutputStream outs = null;
		String resposeStr = null;
		try {
			Runtime rt = Runtime.getRuntime();
			Process pro = rt.exec("scp -r " + fromPath + " " + toPath);
			ins = pro.getErrorStream();
			
			outs = new ByteArrayOutputStream();
			int data = ins.read();
			while (data != -1) {
				outs.write(data);
				data = ins.read();
			}
			resposeStr = outs.toString();
			if(resposeStr == null || resposeStr.equals("")){
				result =true;
			}else{
				LOG.error("SystemCommand:copy error,"+resposeStr);
			}
		} catch (Exception e) {
			LOG.error("SystemCommand:copy error,"+e.getMessage());
			
		}finally{
			if(ins != null){
				try {
					ins.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(outs != null){
				try {
					outs.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		LOG.debug("SystemCommand:copy :end,result:" + result + ",cost(ms):" + (System.currentTimeMillis() - beginTime));
		return result;
	}
	
	
	/****
	 * 备份文件
	 * @param host 主机ip
	 * @param username 用户名
	 * @param password 密码
	 * @param fromPath 源文件
	 * @param toPath 目的路径
	 * @return
	 */
	public static boolean copy(String host,String username, String password,String fromPath, String toPath) {
		LOG.debug("SystemCommand:copy begin,fromPath="+fromPath+",toPath="+toPath);
		long beginTime = System.currentTimeMillis();
		boolean result = false;
		if(StringUtils.isBlank(fromPath) || StringUtils.isBlank(toPath) || StringUtils.isBlank(host) 
				|| StringUtils.isBlank(username) || StringUtils.isBlank(password) ){
			LOG.error("SystemCommand:copy error,input paremeter is blank");
			return result;
		}
	
		try{
			//SSHClientRunner.sshRunCommandWithResultString(host, username, password, "mkdir -p "+toPath);
			SSHClientRunner.sshRunCommandWithResultString(host, username, password, "mkdir -p "+toPath+" && cp -r "+fromPath+" "+toPath);
			result = true;
		}catch(Exception e){
			LOG.error("SystemCommand:copy error,"+e.getMessage(),e);
			return false;
		}
		LOG.debug("SystemCommand:copy :end,result:" + result + ",cost(ms):" + (System.currentTimeMillis() - beginTime));
		return result;
	}
}
